package com.beyondsoft.mcms.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class ValidateUtil {

	public static final String REGEXP_USERNAME = "^[A-Za-z_0-9]{1,15}$";
	public static final String REGEXP_PHONE = "^(\\d{3,4}-)?\\d{7,8}(-\\d{1,4})?$";
	public static final String REGEXP_MOBILE = "^1[0-9]{10}$";
	public static final String REGEXP_EMAIL = "^(\\w+\\.)*\\w+@(\\w+\\.)+\\w+$";
	public static final String REGEXP_DATE = "^(([0-9]{4}|[1-9][0-9]{3})/(((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))/02/29)";

	public static final String REGEXP_AMOUNT = "^\\d+(\\.\\d{1,4})?$";
	public static final String REGEXP_DOUBLE = "^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$";
	public final static String SCIENTIFIC_A = "^[-+]?(\\d+(\\.\\d*)?|\\.\\d+)([eE]([-+]?([012]?\\d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))$";
	public static final String REGEXP_ID = "^[1-9]\\d*$";

	public static boolean isUsername(String str) {
		return Regular(str, REGEXP_USERNAME);
	}

	public static boolean isPhone(String str) {
		return Regular(str, REGEXP_PHONE);
	}

	public static boolean isMobile(String str) {
		return Regular(str, REGEXP_MOBILE);
	}
	
	public static boolean isEmail(String str) {
		return Regular(str, REGEXP_EMAIL);
	}
	
	public static boolean isDate(String str) {
		return Regular(str, REGEXP_DATE);
	}

	public static boolean isNumber(String str) {
		return Regular(str, REGEXP_DOUBLE);
	}
	
	public static boolean isAmount(String str) {
		return Regular(str, REGEXP_AMOUNT);
	}
	
	public static boolean isID(String str) {
		return Regular(str, REGEXP_ID);
	}
	
	public static boolean isScientific(String str) {
		if (StrisNull(str)) {
			return false;
		}
		return Regular(str, SCIENTIFIC_A);
	}
	
	public static synchronized boolean StrisNull(String str) {
		return null == str || str.trim().length() <= 0 ? true : false;
	}
	
	private static boolean Regular(String str, String pattern) {
		if (null == str || str.trim().length() <= 0)
			return false;
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(str);
		return m.matches();
	}

}
